package org.jeecg.modules.demo.tag.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.commoditygroup.entity.CommodityGroup;
import org.jeecg.modules.demo.commoditygroup.service.ICommodityGroupService;
import org.jeecg.modules.demo.commoditymanagement.entity.CommodityManagement;
import org.jeecg.modules.demo.commoditymanagement.service.ICommodityManagementService;
import org.jeecg.modules.demo.tag.entity.Tag;
import org.jeecg.modules.demo.tag.service.ITagService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 商品分组
 */
@Api(tags = "商品分组")
@RestController
@RequestMapping("/tags/tags")
@Slf4j
public class TagsController extends JeecgController<Tag, ITagService> {
    @Autowired
    private ICommodityGroupService commodityGroupService;
    @Autowired
    private ICommodityManagementService commodityManagementService;
    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private ITagService tagService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Tag tag,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId).orderByDesc("id");
        QueryGenerator.initQueryWrapper(queryWrapper, tag, req.getParameterMap());
        Page<Tag> page = new Page<Tag>(pageNo, pageSize);
        IPage<Tag> pageList = tagService.page(page, queryWrapper);

        //List<Tag> listMap = pageList.getRecords();

        return Result.OK(pageList);
    }


    @AutoLog("会员管理-列出标签")
    @ApiOperation(value = "会员管理-列出标签", notes = "会员管理-列出标签（？）")
    @GetMapping({"/listTags"})
    public Result<?> listTags(HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) this.redisUtils.get("StoreId-" + token);
        if (oConvertUtils.isEmpty(storeId))
            return Result.error("请先到首页选择店铺");
        if (storeId.contains("过期"))
            return Result.error("店铺已过期，续费后才可操作");
        List<Tag> list = this.tagService.list((Wrapper) (new QueryWrapper()).eq("store_id", storeId));

        return Result.ok(list);
    }


    /**
     * 添加
     */
    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Tag tag, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        tag.setStoreId(storeId);
        // 当前店铺全部商品分组
        List<Tag> list = tagService.list(new QueryWrapper<Tag>().eq("store_id", storeId).eq("name",tag.getName()));
        if (list.size() > 0) {
            return Result.error("添加失败，标签已经存在！");
        }

        tagService.save(tag);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "商品分组-编辑")
    @ApiOperation(value = "商品分组-编辑", notes = "商品分组-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Tag tag, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        List<Tag> list = tagService.list(new QueryWrapper<Tag>().eq("store_id", storeId).eq("name",tag.getName()) .ne("id", tag.getId()));


        if (list.size() > 0) {
            return Result.error("编辑失败，已有同名标签！");
        }

        tagService.updateById(tag);

        return Result.OK("编辑成功！");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "商品分组-通过id删除")
    @ApiOperation(value = "商品分组-通过id删除", notes = "商品分组-通过id删除")
    @DeleteMapping(value = "/delete")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(@RequestParam("id") String id) {
        tagService.removeById(id);

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "商品分组-批量删除")
    @ApiOperation(value = "商品分组-批量删除", notes = "商品分组-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        this.tagService.removeByIds(idList);

        return Result.OK("批量删除成功!");
    }



}
